package com.xiguaji.tiktok.v1.ins.rank.repository.impl;

import com.xiguaji.boot.toolkit.jdbc.JdbcTemplateHelper;
import com.xiguaji.boot.toolkit.jdbc.Page;
import com.xiguaji.tiktok.v1.ins.rank.dto.InsRkSearch;
import com.xiguaji.tiktok.v1.ins.rank.dto.RankDto;
import com.xiguaji.tiktok.v1.ins.rank.repository.InsRkBloggerBgsMonthExtRepository;
import org.springframework.util.StringUtils;

import java.util.LinkedList;
import java.util.List;

public class InsRkBloggerBgsMonthExtRepositoryImpl implements InsRkBloggerBgsMonthExtRepository {
    @Override
    public Page<RankDto> findPage(InsRkSearch followerSearch) {
        StringBuilder selectSql = new StringBuilder();
        selectSql.append("select r.uid,r.media_count,r.rank,r.tec engagements,");
        selectSql.append("b.username,b.full_name,b.pic_url,b.is_verified,b.nation,b.region,b.category_name as second_category,b.overall_category_name as category_name,b.follower_count ");
        StringBuilder fromSql = new StringBuilder();
        fromSql.append("from ins_rk_blogger_bgs_month r left join ins_blogger_main b on r.uid = b.uid ");
        fromSql.append("where r.datecode = ? ");
        fromSql.append("and r.category_name = ? ");
        List<Object> params = new LinkedList<>();
        params.add(followerSearch.getDate());

        if(!StringUtils.isEmpty(followerSearch.getCategoryName())){
            params.add(followerSearch.getCategoryName());
        }else {
            params.add(" ");
        }
        fromSql.append("order by r.rank ");
        return JdbcTemplateHelper.paginate(RankDto.class,followerSearch.getPage(),followerSearch.getPageSize(),selectSql.toString(),fromSql.toString(),params.toArray());
    }

    @Override
    public List<RankDto> findList(InsRkSearch followerSearch,int size) {
        StringBuilder sql = new StringBuilder();
        sql.append("select r.uid,r.media_count,r.rank,r.tec engagements,");
        sql.append("b.username,b.full_name,b.pic_url,b.is_verified,b.nation,b.region,b.category_name as second_category,b.overall_category_name as category_name,b.follower_count ");
        sql.append("from ins_rk_blogger_bgs_month r left join ins_blogger_main b on r.uid = b.uid ");
        sql.append("where r.datecode = ? ");
        List<Object> params = new LinkedList<>();
        params.add(followerSearch.getDate());
        sql.append("and r.category_name = ? ");

        if(!StringUtils.isEmpty(followerSearch.getCategoryName())){
            params.add(followerSearch.getCategoryName());
        }else {
            params.add(" ");
        }
        sql.append("order by r.rank limit 0,?");
        params.add(size);
        return JdbcTemplateHelper.findEntity(RankDto.class,sql.toString(),params.toArray());
    }
}
